#include "ASScrMain.h"
#include "ASScreenMgr.h"
#include "..\app\asappimginfo.h"
#include "..\control\ASAdvbar.h"
#include "ASScrCategory.h"
#include "mrc_advbar.h"
#include "..\app\ASAppConst.h"
#include "ASScreenShare.h"
#include "ASScrList.h"
#include "..\app\ASLogic.h"
#include "ASWaitingScreen.h"
#include "i18n.h"

static as_category_screen* s_scr_category;
static as_button* s_btns[9];
static as_category_bt s_btninfo[9];
static ASINT s_cur_category;

static void as_scr_category_add_head(as_screen* screen_p)
{
    s_scr_category->head_height = as_screen_create_adv_titlebar(screen_p, STR_CATEGORY);
}

static void as_scr_set_btn_info(ASINT idx, ASSTR img_u, ASSTR img_d, ASSTR t_cn, ASINT str_cn, btn_cb cb)
{
    s_btninfo[idx].img_d = img_d;
    s_btninfo[idx].img_u = img_u;
    s_btninfo[idx].t_cn = t_cn;
    s_btninfo[idx].str_cn = str_cn;
    s_btninfo[idx].cb = cb;
}

static void as_scr_category_entry_list(ASINT categoryid)
{
    //as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    if (categoryid < ENUM_CATEGORY_SIXTY || categoryid >= ENUM_CATEGORY_COUNT)
        return;

    //scr_mgr->delete_and_pop_screen();
    as_show_waiting_screen(TEXT_WAITING);
    as_scr_list_show(categoryid);
}
static void as_scr_category_sixty_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_SIXTY);
}

static void as_scr_category_butterfly_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_BUTTERFLY);
}

static void as_scr_category_cowgirl_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_COWGIRL);
}

static void as_scr_category_cunni_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_CUNNI);
}

static void as_scr_category_doggy_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_DOGGY);
}
static void as_scr_category_exotic_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_FACETOFACE);
}

static void as_scr_category_f2f_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_FACETOFACE);
}

static void as_scr_category_fell_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_FELL);
}

static void as_scr_category_spoon_evt(void)
{
    as_scr_category_entry_list(ENUM_CATEGORY_SPOON);
}

static void as_scr_category_init_btns(void)
{
    as_scr_set_btn_info(0, IMG_CATE_SIXTY, IMG_CATE_SIXTY_D, IMG_T_SIXTY_CN, STR_CATE_SIXTY_CN, as_scr_category_sixty_evt);
    as_scr_set_btn_info(1, IMG_CATE_BUTTERFLY, IMG_CATE_BUTTERFLY_D, IMG_T_BUTTERFLY_CN, STR_CATE_BUTTERFLY_CN, as_scr_category_butterfly_evt);
    as_scr_set_btn_info(2, IMG_CATE_COWGIRL, IMG_CATE_COWGIRL_D, IMG_T_COWGIRL_CN, STR_CATE_COWGIRL_CN, as_scr_category_cowgirl_evt);
    as_scr_set_btn_info(3, IMG_CATE_CUNNI, IMG_CATE_CUNNI_D, IMG_T_CUNNI_CN, STR_CATE_CUNNI_CN, as_scr_category_cunni_evt);
    as_scr_set_btn_info(4, IMG_CATE_DOGGY, IMG_CATE_DOGGY_D, IMG_T_DOGGY_CN, STR_CATE_DOGGY_CN, as_scr_category_doggy_evt);
    as_scr_set_btn_info(5, IMG_CATE_EXOTIC, IMG_CATE_EXOTIC_D, IMG_T_EXOTIC_CN, STR_CATE_EXOTIC_CN, as_scr_category_exotic_evt);
    as_scr_set_btn_info(6, IMG_CATE_FACETOFACE, IMG_CATE_FACETOFACE_D, IMG_T_F2F_CN, STR_CATE_F2F_CN, as_scr_category_f2f_evt);
    as_scr_set_btn_info(7, IMG_CATE_FELL, IMG_CATE_FELL_D, IMG_T_FELL_CN, STR_CATE_FELL_CN, as_scr_category_fell_evt);
    as_scr_set_btn_info(8, IMG_CATE_SPOONING, IMG_CATE_SPOONING_D, IMG_T_SPOON_CN, STR_CATE_SPOON_CN, as_scr_category_spoon_evt);
}

static void as_scr_category_add_menu(as_screen* screen_p)
{
#define SIDE_GAP		20
    ASINT w, h, intervalx, intervaly, i;
    ASUINT param[6];
    ASWCHAR tmp[30], tmp2[30];
    as_rect area;
    ASSTR strTitle;
    as_global_config* config_p = as_get_global_config();
    as_scr_category_init_btns();
    as_get_internal_img_res_size(IMG_CATE_SIXTY, &w, &h);
    intervalx = (as_get_lcd_width() - SIDE_GAP * 2 - w * 3) / 2;
    intervaly = (as_get_lcd_height() - s_scr_category->head_height - h * 3 - BUTTON_BAR_H) / 3;

    for (i = 0; i < 9; i++)
    {
        area.x = SIDE_GAP + (intervalx + w) * (i % 3);
        area.y = s_scr_category->head_height + (intervaly + h) * (i / 3);
        area.w = w;
        area.h = h;
        as_memset(tmp, 0, sizeof(tmp));
        as_memset(tmp2, 0, sizeof(tmp2));
        as_ascii_to_ucs2(tmp, s_btninfo[i].img_u);
        as_ascii_to_ucs2(tmp2, s_btninfo[i].img_d);
        param[0] = AS_IMAGE_BUTTON;
        param[1] = (ASUINT)tmp;
        param[2] = (ASUINT)tmp2;
        param[3] = (ASUINT)NULL;
        param[4] = AS_IMAGE_FROM_RES;
        s_btns[i] = (as_button*)screen_p->add_ctrl(screen_p, AS_CTRL_BUTTON, area, 5, (void**)param);
        s_btns[i]->set_cb_by_status((as_control*)s_btns[i], AS_BUTTON_UP, s_btninfo[i].cb);
        area.y += IMG_CATE_SIXTY_H;
        strTitle = s_btninfo[i].t_cn;
        param[0] = AS_IMAGE_FROM_RES;
        param[1] = (ASUINT)as_ucs2_from_ascii(strTitle);
        param[2] = ASFALSE;
        screen_p->add_ctrl(screen_p, AS_CTRL_IMAGE, area, 3, (void**)param);
    }

    s_btns[0]->change_status((as_control*)s_btns[0], AS_BUTTON_DOWN);
}

static void as_scr_category_lsk_evt(void)
{
    as_scr_category_entry_list(s_cur_category);
}
static void as_scr_category_rsk_evt(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    scr_mgr->delete_and_pop_screen();
    as_scr_main_show();
}

static void as_scr_category_create_softkey(as_screen* screen_p)
{
    as_screen_create_softkey_bar(screen_p, TEXT_ENTER, TEXT_BACK, as_scr_category_lsk_evt, as_scr_category_rsk_evt);
}

static ASBOOL as_scr_category_focus_evt(as_screen* screen_p, ASINT event, ASINT keycode)
{
    ASINT isel = s_cur_category;

    if (keycode == AS_KEY_UP || keycode == AS_KEY_VUP || keycode == AS_KEY_DOWN || keycode == AS_KEY_VDOWN || keycode == AS_KEY_LEFT || keycode == AS_KEY_RIGHT)
    {
        if (event == AS_EVT_KEY_UP)
        {
            if (keycode == AS_KEY_UP || keycode == AS_KEY_VUP || keycode == AS_KEY_LEFT)
            {
                s_btns[isel]->reset_status((as_control*)s_btns[isel]);
                isel--;

                if (isel < 0)
                    isel = (isel + ENUM_CATEGORY_COUNT) % ENUM_CATEGORY_COUNT;

                s_cur_category = isel;
                s_btns[isel]->change_status((as_control*)s_btns[isel], AS_BUTTON_DOWN);
            }
            else
            {
                s_btns[isel]->reset_status((as_control*)s_btns[isel]);
                isel++;

                if (isel >= ENUM_CATEGORY_COUNT)
                    isel = isel % ENUM_CATEGORY_COUNT;

                s_cur_category = isel;
                s_btns[isel]->change_status((as_control*)s_btns[isel], AS_BUTTON_DOWN);
            }

            return ASTRUE;
        }
        else
            return ASFALSE;
    }
    else
        return ASFALSE;
}


void as_scr_category_init(as_screen* screen_p)
{
    s_scr_category = (as_category_screen*)screen_p;
    screen_p->set_bg_color(screen_p, SCREEN_BG_COLOR);
    as_scr_category_add_head(screen_p);
    as_scr_category_add_menu(screen_p);
    as_scr_category_create_softkey(screen_p);
    screen_p->set_focus_hdlr(screen_p, as_scr_category_focus_evt);
}

void as_scr_category_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}

void as_scr_category_show()
{
    as_screen* scr_p;
    as_ret ret;
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    ret = scr_mgr->new_and_push_screen(AS_SCREEN_CATEGORY);
    scr_p = scr_mgr->get_cur_screen();

    if(scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
}